﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Linq;
using MII_FIF.Common;

namespace MII_FIF.Layouts.MII_FIF
{
    public partial class ViewStorage : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.DeviceManagementGroupName))
            {
                ((Label)Header.FindControl("lblHeader")).Text = "Device Management - Storage";
                btnDelete.Click += btnDelete_Click;
                GridDataTable.RowCommand += GridDataTable_RowCommand;
                btnAdd.Click += btnAdd_Click;
                if (!IsPostBack)
                {
                    load();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/DeviceManagement/FormStorage.aspx"));
        }

        void GridDataTable_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/DeviceManagement/FormStorage.aspx?ID=" + e.CommandArgument));
            }
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            bool delete = false;
            DataTable datatable = (DataTable)ViewState["datatable"];
            InventoryManagement.Inventory_Storage storage = new InventoryManagement.Inventory_Storage();
            int stat = 0;
            foreach (GridViewRow row in GridDataTable.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chk");
                if (chk != null)
                    if (chk.Checked)
                    {
                        delete = true;
                        break;
                    }
            }

            if (delete)
            {
                for (int i = GridDataTable.Rows.Count - 1; i > -1; i--)
                {
                    CheckBox chk = (CheckBox)GridDataTable.Rows[i].FindControl("chk");
                    if (chk != null)
                        if (chk.Checked)
                        {                                                        
                            storage.ID = Guid.Parse(datatable.Rows[i]["ID"].ToString());

                            InventoryManagement.CR_Storage crStorage = new InventoryManagement.CR_Storage();
                            crStorage.ID_Storage = storage.ID;
                            crStorage.wfStatus = word.WaitingforApproval;
                            DataTable dtStorage = crStorage.SelectByParam();
                            if (dtStorage.Rows.Count > 0)
                            {
                                lblError.Text += "Cannot delete item there is pending Approval Change Request";
                                break;
                            }

                            storage.Select();
                            storage.IsDeleted = true;
                            stat = storage.Update();
                            datatable.Rows.RemoveAt(i);
                            if (stat < 0)
                            {
                                lblError.Text += "Delete Failed"; break;
                            }
                        }
                }
                GridDataTable.DataSource = datatable;
                GridDataTable.DataBind();
                ViewState["datatable"] = datatable;
            }
            else
            {
                lblError.Text += "Please check at least 1 item to delete <br />";
            }
        }

        void load()
        {
            /*InventoryManagement.Inventory_Storage storage = new InventoryManagement.Inventory_Storage();
            DataTable datatable = storage.SelectByParam();
            InventoryManagement.Rack_Number rack = new InventoryManagement.Rack_Number();
            DataTable dtRack = rack.SelectByParam();
            InventoryManagement.Location location = new InventoryManagement.Location();
            DataTable dtLocation = location.SelectByParam();

            var joinedTable = from t1 in datatable.AsEnumerable()
                              join t2 in dtRack.AsEnumerable() on t1.Field<Guid>("ID_RackNumber") equals t2.Field<Guid>("ID")
                              join t3 in dtLocation.AsEnumerable() on t1.Field<Guid>("ID_DataCabang") equals t3.Field<Guid>("ID")
                              select new
                              {
                                  ID = t1.Field<Guid>("ID"),
                                  LabelName = t1.Field<string>("LabelName"),
                                  Location = t3.Field<string>("Lokasi"),
                                  RackNumber = t2.Field<string>("Title"),
                                  StartFrom = t1.Field<string>("StartFrom")
                              };

            GridDataTable.DataSource = joinedTable;
            ViewState["datatable"] = joinedTable;
            GridDataTable.DataBind();*/

            InventoryManagement.Inventory_Storage storage = new InventoryManagement.Inventory_Storage();
            storage.IsDeleted = false;
            DataTable datatable = storage.SelectByParam();
            datatable.Columns.Add("Location");
            datatable.Columns.Add("RackNumber");
            foreach (DataRow row in datatable.Rows)
            {
                InventoryManagement.Location location = new InventoryManagement.Location();
                location.ID = StringUtil.ToGuid(row["ID_DataCabang"].ToString());
                location.Select();
                InventoryManagement.Rack_Number rackNumber = new InventoryManagement.Rack_Number();
                rackNumber.ID = StringUtil.ToGuid(row["ID_RackNumber"].ToString());
                rackNumber.Select();
                row["Location"] = location.Lokasi;
                row["RackNumber"] = rackNumber.Title;
            }
            GridDataTable.DataSource = datatable;
            ViewState["datatable"] = datatable;
            GridDataTable.DataBind();
        }
    }
}
